import request from '@/utils/request'
const Base64 = require('js-base64').Base64
const clientId = 'manager-api'
const clientSecret = '123456'
const encode = Base64.encode(clientId + ':' + clientSecret)
const authorization = 'Basic ' + encode

export function login(form) {
  console.log('to login request', form)
  const params = new URLSearchParams()
  params.append('username', form.username)
  params.append('password', form.password)
  params.append('grant_type', 'password')
  params.append('scope', 'read')
  return request({
    url: '/vue-admin-template/oauth/token',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': authorization
    },
    params
  })
}

export function refreshToken(refreshToken) {
  console.log('refresh token request', refreshToken)
  const params = new URLSearchParams()
  params.append('refresh_token', refreshToken)
  params.append('grant_type', 'refresh_token')
  params.append('scope', 'read')
  return request({
    url: '/auth/oauth/token',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': authorization
    },
    params
  })
}

export function getInfo() {
  return request({
    url: '/vue-admin-template/api/v1/user/username',
    method: 'get'
  })
}

export function logout() {
  return request({
    url: '/vue-admin-template/user/logout',
    method: 'post'
  })
}
